package com.daykw.wenshu;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import com.daykw.wenshu.task.AdhocYearDiffJob;
import com.daykw.wenshu.task.ArticleHtmlJob;
import com.daykw.wenshu.task.ArticleJob;
import com.daykw.wenshu.task.ListAdhocUrlJob;
import com.daykw.wenshu.task.ListDailySubUrlJob;
import com.daykw.wenshu.task.ListDailyUrlJob;
import com.daykw.wenshu.utils.Const;
import com.daykw.wenshu.utils.SpringContextHolder;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App {
	private static Logger logger = Logger.getLogger(App.class);

	public static void main(String[] args) {
		String node = null;
		if (args.length != 0 && args != null && StringUtils.isNotEmpty(args[0])) {
			node = args[0];
			Const.Node = node;
		}
		logger.info("当前节点node:" + node);
		SpringApplicationBuilder builder = new SpringApplicationBuilder(App.class);

		builder.web(WebApplicationType.SERVLET).run();

		logger.info("##########spring boot 启动成功##########");
		Const c = SpringContextHolder.getBean(Const.class);

	

		// ScheduledExecutorService scheduled7 =
		// Executors.newSingleThreadScheduledExecutor();
		// scheduled7.scheduleAtFixedRate(new PaserArticleJob(), 0, 10,
		// TimeUnit.SECONDS);

		if (c.getDaily_list_thread()) {
			ScheduledExecutorService scheduled1 = Executors.newSingleThreadScheduledExecutor();
			scheduled1.scheduleAtFixedRate(new ListDailyUrlJob(), 0, 30, TimeUnit.SECONDS);
		}
		if (c.getAdhoc_list_thread()) {
			ScheduledExecutorService scheduled1 = Executors.newSingleThreadScheduledExecutor();
			scheduled1.scheduleAtFixedRate(new ListAdhocUrlJob(), 0, 30, TimeUnit.SECONDS);
		}

		if (c.getDaily_list_sub_thread()) {
			ScheduledExecutorService scheduled3 = Executors.newSingleThreadScheduledExecutor();
			scheduled3.scheduleAtFixedRate(new ListDailySubUrlJob(), 0, 30, TimeUnit.SECONDS);

		}
		if (c.getArticle_thread()) {
			// ScheduledExecutorService scheduled7 =
			// Executors.newSingleThreadScheduledExecutor();
			// scheduled7.scheduleAtFixedRate(new ArticleAdhocJob(), 0, 15,
			// TimeUnit.SECONDS);

			ScheduledExecutorService scheduled6 = Executors.newSingleThreadScheduledExecutor();
			scheduled6.scheduleAtFixedRate(new ArticleJob(), 0, 30, TimeUnit.SECONDS);

			for (int i = 0; i < c.getArticle_num(); i++) {

				ArticleHtmlJob thread = new ArticleHtmlJob();
				thread.start();

				// ArticleAdhocHtmlJob athread = new ArticleAdhocHtmlJob();
				// athread.start();
			}

		}
	}
}
